/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb;

import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;

/**
 *
 * @author Lella
 */
@Stateless
@LocalBean
public class BusinessLogic {
    @EJB
    private CameraFacade cameraFacade;
    
    public void crea(int numLetti){
        Camera c = new Camera();
        c.setNumeroLetti(numLetti);
        cameraFacade.create(c);
    } 
    
    public int camereLibere(int numLetti){
        String sql = "SELECT a FROM Camera a WHERE a.numeroLetti=" + numLetti +" AND a.prenotata=0";//se non funziona togli apici a false
        return cameraFacade.queryPersonalizzata(sql).getResultList().size();
    }
    
    public int prenotaCamera(int numLetti, String nome, String cognome, String username){
        int done = 0;
        if(camereLibere(numLetti) >= 1){
            String sql = "SELECT a FROM Camera a WHERE a.numeroLetti=" + numLetti +" AND a.prenotata=0";//se non funziona togli apici a false
            Camera c = (Camera)cameraFacade.queryPersonalizzata(sql).getResultList().get(0);
            sql = "UPDATE Camera a SET a.prenotata=1, a.nomePrenotazione='" + nome +"', a.cognomePrenotazione='"+ cognome +"'";
            if(username != null)
                sql += ", a.username ='" + username + "'";
            sql += " WHERE a.id=" + c.getId();
            done = cameraFacade.queryPersonalizzata(sql).executeUpdate();
        }
        return done;
    }
}
